package org.infosys.basic.controller;

import org.infosys.basic.exception.BusinessException;
import org.infosys.basic.service.ILoginService;
import org.infosys.basic.utils.AjaxResult;
import org.infosys.basic.vo.LoginVo;
import org.infosys.user.dto.WxDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
    * @Title: accountLogin
    * @Description: 账号登录
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/7 16:20
    * @Parameters: [loginVo]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginVo loginVo) {
        try {
            HashMap<String, Object> map = loginService.accountLogin(loginVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，请稍后再试！").setSuccess(false);
        }
    }

    /**
    * @Title: weChatLogin
    * @Description: 微信登录
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/10 17:45
    * @Parameters: [wxDto]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult weChatLogin(@RequestBody WxDto wxDto) {
        try {
            return loginService.wechatLogin(wxDto);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，请稍后再试！").setSuccess(false);
        }
    }

    /**
    * @Title: binder
    * @Description: 微信登录时账号绑定
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/10 20:06
    * @Parameters: [wxDto]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WxDto wxDto) {
        try {
            HashMap<String, Object> map = loginService.binder(wxDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，请稍后再试！").setSuccess(false);
        }
    }
}
